package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.mapper.FlashPromotionProductRelationMapper;
import com.itheima.mall.mapper.FlashPromotionSessionMapper;
import com.itheima.mall.pojo.FlashPromotionSession;
import com.itheima.mall.service.SmsFlashPromotionSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<FlashPromotionSessionMapper,FlashPromotionSession> implements SmsFlashPromotionSessionService {

    @Autowired
    private FlashPromotionSessionMapper flashPromotionSessionMapper;



    /**
     * 获取全部场次
     */
    @Override
    public List<FlashPromotionSession> listAll() {
        return flashPromotionSessionMapper.selectList(null);
    }

    /**
     * 获取全部可选场次及其数量
     */
    @Override
    @Transactional
    public List<FlashPromotionSession> selectList(Integer flashPromotionId) {
        //去查关系表,获取可选场次id
        List<Long> fpsId = flashPromotionSessionMapper.selectRelationByFpId(flashPromotionId);
        if (CollUtil.isEmpty(fpsId)) {
            return new ArrayList<>();
        }
        //获取当前场次下商品数量
        List<Integer> counts = flashPromotionSessionMapper.getProductionCounts(fpsId);
        //用fpsId去查fps表
        List<FlashPromotionSession> flashPromotionSessions = flashPromotionSessionMapper.selectByIds(fpsId);
        AtomicInteger index = new AtomicInteger();
        flashPromotionSessions = flashPromotionSessions.stream().map(item -> {
            Integer count = counts.get(index.get());
            item.setProductCount(count);
            index.getAndIncrement();
            return item;
        }).collect(Collectors.toList());

        return flashPromotionSessions;

    }

    /**
     * 添加场次
     */
    @Override
    public void create(FlashPromotionSession promotionSession) {
        //添加fps表
        flashPromotionSessionMapper.insert(promotionSession);
    }

    /**
     * 删除场次
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //删除fps表
        flashPromotionSessionMapper.deleteById(id);
        //删除关联关系表
        flashPromotionSessionMapper.deleteFpprById(id);
    }

    /**
     * 修改启用状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<FlashPromotionSession> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(status != null,FlashPromotionSession::getStatus,status);
        updateWrapper.eq(id != null,FlashPromotionSession::getId,id);
        flashPromotionSessionMapper.update(null,updateWrapper);
    }

    /**
     * 修改场次
     */
    @Override
    public void update(Long id, FlashPromotionSession promotionSession) {
        promotionSession.setId(id);
        flashPromotionSessionMapper.updateById(promotionSession);
    }

    /**
     * 获取场次详情
     */
    @Override
    public FlashPromotionSession flashSession(Long id) {
        return flashPromotionSessionMapper.selectById(id);
    }


}
